let log = require("./../../util/loginfo").getInstand;
let CustomMutex = require("./custom_mutex")
class ThreadSafeLoginList {
    constructor() {
        this._loginList = [];
        this._mutex = new CustomMutex();
    }

    // 添加用户到登录列表
    addUser(user) {
        return this._mutex.runExclusive(() => {
            // 先删除已存在的相同用户
            this._deleteUserInternal(user.userName);
            
            user.state = 1;
            user.AutoOutCount = 0;
            this._loginList.push(user);
            
            log.info(`addUser account ${user.userName}, username ${user.userName}, state ${user.state}`);
            return true;
        });
    }

    // 检查用户状态
    getLoginState(useraccount, state) {
        return this._mutex.runExclusive(() => {
            for (let i = 0; i < this._loginList.length; i++) {
                if (this._loginList[i].userName === useraccount && this._loginList[i].state === state) {
                    log.info(`getLoginState account ${useraccount}, username ${this._loginList[i].userName}, state ${state}`);
                    return true;
                }
            }
            return false;
        });
    }

    // 删除用户
    deleteUser(useraccount) {
        return this._mutex.runExclusive(() => {
            return this._deleteUserInternal(useraccount);
        });
    }

    // 内部删除方法（不获取锁）
    _deleteUserInternal(useraccount) {
        const idx = this._loginList.findIndex(user => user.userName === useraccount);
        if (idx !== -1) {
            log.info(`delete login list account ${useraccount}, username ${this._loginList[idx].userName}`);
            this._loginList.splice(idx, 1);
            return true;
        }
        return false;
    }

    // 获取所有用户
    getAllUsers() {
        return this._mutex.runExclusive(() => {
            return [...this._loginList]; // 返回副本以避免外部修改
        });
    }

    // 根据条件查找用户
    findUser(predicate) {
        return this._mutex.runExclusive(() => {
            return this._loginList.find(predicate);
        });
    }

    // 根据条件过滤用户
    filterUsers(predicate) {
        return this._mutex.runExclusive(() => {
            return this._loginList.filter(predicate);
        });
    }

    // 更新用户状态
    updateUserState(useraccount, newState) {
        return this._mutex.runExclusive(() => {
            const user = this._loginList.find(u => u.userName === useraccount);
            if (user) {
                const oldState = user.state;
                user.state = newState;
                log.info(`updateUserState account ${useraccount}, state ${oldState} -> ${newState}`);
                return true;
            }
            return false;
        });
    }

    // 获取列表长度
    getLength() {
        return this._mutex.runExclusive(() => {
            return this._loginList.length;
        });
    }

    // 清空列表
    clear() {
        return this._mutex.runExclusive(() => {
            this._loginList = [];
            return true;
        });
    }

    // 批量操作（执行自定义函数，自动处理锁）
    withLock(callback) {
        return this._mutex.runExclusive(() => {
            return callback(this._loginList);
        });
    }

    // 获取当前锁状态（调试用）
    isLocked() {
        return this._mutex.isLocked();
    }
}

module.exports = ThreadSafeLoginList;